const db = wx.cloud.database();
const config = require('../config')
Page({
  data: {
    orderList: [],
    state: 1,
    confirmBtn: { content: '确定', variant: 'base' },
    dialogKey: '',
    showConfirm: false,
    showWarnConfirm: false,
    showTooLongBtnContent: false,
    showMultiBtn: false,
    multiBtnList: [
      { content: '次要按钮', theme: 'light' },
      { content: '次要按钮', theme: 'light' },
      { content: '主要按钮', theme: 'primary' },
    ],
  },
  onShow(options) {
    // this.fetchOrders();
    const totalMoney = wx.getStorageSync('totalPrice');
    this.setData({
      totalMoney:totalMoney
    })
    this.getOrderList();
  },
  // 选择订单类型
  chooType(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      state: type
    });
    console.log(this.data.state)
    this.getOrderList(type);
  },
  async getOrderList(type) {
    const { API_BASE_URL } = config;
    console.log("type:"+type);
    wx.request({
      url: `${API_BASE_URL}/`
    })

    this.setData({
      orderList:res.data
    })
  },
  toPay(e){
    const id=e.currentTarget.dataset.id
    wx.showModal({  
      title: '确认支付',  
      content: '您确定要支付该订单吗？',  
      success: function(res) {  
        if (res.confirm) {  
          // 用户点击确定  
          console.log('用户点击确定，执行支付操作', id);  
          // 这里可以调用支付接口，或者跳转到支付页面  
          // 例如：wx.navigateTo({url: `/pages/pay/pay?id=${itemId}`})  
          db.collection('shop_order').doc(id)
          .update({
            data:{
              state:0
            }
          }).then(res=>{
            console.log(res)
          })
        } else if (res.cancel) {  
          // 用户点击取消  
          console.log('用户点击取消');  
        }  
      }  
    })  
  },
  tocancel(e){
    const id=e.currentTarget.dataset.id
    db.collection('shop_order').doc(id)
    .update({
      data:{
        state:1
      }
    }).then(res=>{
      console.log(res)
    })
  },
  toreceived(e){
    const id=e.currentTarget.dataset.id
    db.collection('shop_order').doc(id)
    .update({
      data:{
        state:3
      }
    }).then(res=>{
      console.log(res)
    })
  },
  showDialog(e) {
    const { key } = e.currentTarget.dataset;
    this.setData({ [key]: true, dialogKey: key });
  },
  closeDialog() {
    const { dialogKey } = this.data;
    this.setData({ [dialogKey]: false });
  },
});
